package com.tvunetworks.center.user.service.impl;

import com.tvunetworks.center.common.constant.LocalConstant;
import com.tvunetworks.center.user.constant.UserConstant;
import com.tvunetworks.center.user.mapper.GridEventBindRecordMapper;
import com.tvunetworks.center.user.model.GridEventBindRecord;
import com.tvunetworks.center.user.model.User;
import com.tvunetworks.center.user.service.GridEventBindRecordService;
import com.tvunetworks.center.user.service.UserDeviceService;
import com.tvunetworks.center.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: tvucc-aggregator
 * @description: GridEventBindRecordServiceImpl
 * @author lebronchen
 * @create: 2020-01-03 16:46
 **/
@Service
@Slf4j
public class GridEventBindRecordServiceImpl extends BaseService<GridEventBindRecord> implements GridEventBindRecordService {

    @Autowired
    private UserService userService;
    @Autowired
    private UserDeviceService userDeviceService;
    @Autowired
    private GridEventBindRecordMapper gridEventBindRecordMapper;

    @Override
    public int addUserDevice(String gridEventId, String userId, String peerId) {
        userDeviceService.addUserDevice(userId, peerId);
        this.addRecord(gridEventId, userId);
        User user = userService.getById(userId);
        String role = user.getRole();
        if (LocalConstant.UserRole.USER.equals(role)) {
            // 如果为 User 添加 Device，需要考虑父帐户是否有这个设备
            String userParentId = user.getUserParentId();
            if (StringUtils.isNotBlank(userParentId)) {
                boolean parentExistDevice = userDeviceService.isUserDeviceExist(userParentId, peerId);
                if (!parentExistDevice) {
                    // 如果父帐户 没有这个设备，那么也需要为父帐户添加设备
                    userDeviceService.addUserDevice(userParentId, peerId);
                    this.addRecord(gridEventId, userParentId);
                } else {
                    int count = this.countUserDeviceExistBindRecord(userParentId, peerId);
                    if (count > 0) {
                        // 如果父帐户 有这个设备，但是是别的 GridEvent 产生的 UserDevice，当前 GridEvent 也要记录 GridEventBindRecord
                        this.addRecord(gridEventId, userParentId);
                    }
                }
            }
        }
        return 0;
    }

    @Override
    public int removeUserDevice(String gridEventId, String userId, String gridPeerId) {
        // 当前存在的通过 GridEvent 产生该 UserDevice 的 event 数量
        int count = this.countUserDeviceExistBindRecord(userId, gridPeerId);
        if (count != 1) {
            // 并非只有当前这一个 GridEvent 产生了当前的 UserDevice，那么任务结束了，不需要移除绑定关系
            return 1;
        }
        // 如果只有当前这一个 GridEvent 产生了当前的 UserDevice，那么任务结束了，需要移除绑定关系
        // 移除当前用户的 UserDevice 绑定关系
        userDeviceService.removeUserDevice(userId, gridPeerId);
        User user = userService.getById(userId);
        String role = user.getRole();
        if (LocalConstant.UserRole.USER.equals(role)) {
            // 如果为 User 添加 Device，需要考虑父帐户是否有这个设备
            String userParentId = user.getUserParentId();
            if (StringUtils.isNotBlank(userParentId)) {
                // 父级用户 当前存在的通过 GridEvent 产生该 UserDevice 的 event 数量
                count = this.countUserDeviceExistBindRecord(userParentId, gridPeerId);
                if (count == 1) {
                    // 如果只有当前这一个 GridEvent 产生了当前的 UserDevice，那么任务结束了，需要移除绑定关系
                    // 移除父级用户的 UserDevice 绑定关系
                    userDeviceService.removeUserDevice(userParentId, gridPeerId);
                }
            }
        }
        return 0;
    }

    @Override
    public int countUserDeviceExistBindRecord(String userId, String peerId) {
        int count = gridEventBindRecordMapper.countActiveEventBindRecord(UserConstant.GridEvent.Status.START, UserConstant.GridEvent.BindStatus.EVENT_START_ADD_BIND, userId, peerId);
        return count;
    }

    private void addRecord(String gridEventId, String userId){
        GridEventBindRecord record = new GridEventBindRecord()
                .setGridEventId(gridEventId)
                .setUserId(userId);
        super.insert(record);
    }
}
